﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System.Data;
namespace ManagerWed.Controllers
{
    public class SalesManController : Controller
    {
        //
        // GET: /Level/
        public ActionResult SalesMan()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");

            return View();
        }
        #region PC模糊查询
        public JsonResult query_salesman(int pageindex, int pagesize)
        {
            try
            {
                SalesManBLL bll = new SalesManBLL();
                List<salesman> list = bll.GetSalesManBycondtion(pageindex, pagesize);
                object pagecount = bll.GetSalesManBycondtionCount();
                return Json(new { MessageCode = "0", LevelList = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }

        }
        #endregion
        public JsonResult GetSalesMan(int id)
        {
            SalesManBLL bll = new SalesManBLL();
            salesman model = bll.GetModel(id);
            return Json(new { model = model }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 依次递增业务员编号信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetMaxCode() {
            SalesManBLL bll = new SalesManBLL();
            DataTable tbl= bll.GetMaxCode();
            salesman model = new salesman();
            if (string.IsNullOrEmpty(tbl.Rows[0]["code"].ToString()))
                model.code = "01";
            else
                model.code = tbl.Rows[0]["code"].ToString().PadLeft(2, '0');
               
            return Json(new { model = model }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult OpeaSalesMan()
        {
            try
            {
                string userName =Encrypt.Decode(CookieHelper.GetValue("pcnfs_user_name"));
                SalesManBLL bll = new SalesManBLL();
                int id = Request["id"] != null ? int.Parse(Request["id"].ToString()) : 0;
                string opea = Request["opea"] != null ? Request["opea"] : "";
               
                string Name = Request["name"] != null ? Request["name"] : "";
                string code = Request["code"] != null ? Request["code"] : "";
               
                if (!bll.GetSingle(id, Name,code))
                {
                    salesman model = new salesman { id = id, name = Name, code = code, valid = 1,createusername= userName };
                    bool flag;
                    if (opea == "add")
                    {
                        flag = bll.InsertSalesMan(model, code);
                    }
                    else
                    {
                        flag = bll.updateSalesMan(model, code);
                    }
                    if (flag)
                    {
                        return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {

                    return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

            }


        }
        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int id = Request["id"] != "" ? int.Parse(Request["id"]) : 0;
                if (bll.com_delelte(id, "t_salesman", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }


        }
        #endregion

        #region 绑定下拉框信息
        public JsonResult GetSalesManName()
        {
            try
            {
                SalesManBLL bll = new SalesManBLL();
                List<salesman> list = bll.GetModelList(" valid=1");
                return Json(new { MessageCode = "0", list = list }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

	}
}